package com.penghaisoft.microwms.producthand.web.controller.insidewarehouse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProStorageSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

@RestController
@RequestMapping(value = "/pro/lockhand")
public class ProLockHandController extends BaseController {
	@Autowired
	private IProStorageSnService proStorageSnService;


	    @PostMapping("lockmessage")
	    public HandResp lockMessage(@RequestBody ProStorageSn proStorageSn){
	        HandResp result = new HandResp();
	        //Code先置为1SUCCESS
	        result.setCode(Constant.SUCCESS);
	        //根据条码查询
	        proStorageSn.setActiveFlag("1");
	        List<ProStorageSn> list = proStorageSnService.queryByAny(proStorageSn);
	        if(list.size() <= 0){
	        	result.setMsg("该条码不存在");
	        	result.setCode(Constant.FAIL);
	        }else{
	        	if(list.get(0).getBarcodeStatus().equals("2")){
	        		result.setMsg("该条码已出库");
	            	result.setCode(Constant.FAIL);
	        	}else if(list.get(0).getBarcodeStatus().equals("1")){
	        		result.setMsg("该条码已冻结");
	            	result.setCode(Constant.FAIL);
	        	}
	        	else{
	        		//返回信息
	        		result.setList(list);
	        		result.setMsg("该条码可进行冻结，请确认");
	        	}
	        }
	        return result;
	    }
	    

	    @PostMapping("lockconfig")
	    public HandResp lockConfig(@RequestBody ProStorageSnDto proStorageSnDto){
	        HandResp result = new HandResp();
	        //Code先置为1SUCCESS
	        result.setCode(Constant.SUCCESS);
	        ProStorageSn barcodeStorageSn = new ProStorageSn();
	        barcodeStorageSn.setBarcode(proStorageSnDto.getBarcode());
	        barcodeStorageSn.setActiveFlag("1");
	        //根据条码查询
	        List<ProStorageSn> list = proStorageSnService.queryByAny(barcodeStorageSn);
	        if(list.size() <= 0){
	        	result.setMsg("该条码不存在");
	        	result.setCode(Constant.FAIL);
	        }else{
	        	if(list.get(0).getBarcodeStatus().equals("2")){
	        		result.setMsg("该条码已出库");
	            	result.setCode(Constant.FAIL);
	        	}else if(list.get(0).getBarcodeStatus().equals("1")){
	        		result.setMsg("该条码已冻结");
	            	result.setCode(Constant.FAIL);
	        	}else{
	        		//开始冻结
	    			Resp resp = new Resp();
	    			ProStorageSn proStorageSn = new ProStorageSn();
	    			proStorageSn.setStorageSnId(list.get(0).getStorageSnId());
	    			proStorageSn.setBarcode(proStorageSnDto.getBarcode());
	    			proStorageSn.setBarcodeStatus("1");//冻结
	    			proStorageSn.setLockReason(proStorageSnDto.getLockReason());//0手工冻结1质量冻结
	    			proStorageSn.setLastModifiedBy(proStorageSnDto.getLoginName());
	    			proStorageSn.setGmtModified(new Date());
	    			resp = proStorageSnService.updateProStorageSn(proStorageSn);
	    			result.setCode(resp.getCode());
	    			if(resp.getCode().equals("0")){
	    				result.setMsg(resp.getMsg());	
	    			}
	        	}
	        }
	        return result;
	    }
}
